/**
 * Created by YY on 16/8/12.
 */
var entityCollection = require('../../../Base/server/controllers/EntityCollection');

//查找所有部门
exports.getAllDepartment = function (completeFun) {
    var entity = entityCollection.getEntity("Department");
    entity.find({IsEffective: true}, null, null, function (err, records) {
        if (err) {
            var newErr = new Error();
            newErr.leval = 9;
            newErr.title = '查询部门错误';
            newErr.message = '请检查查询参数！';
            completeFun(newErr);
        } else {
            completeFun(null, records);
        }
    });
};
//查找部门by _id
exports.findDepartmentById = function (id, completeFun) {
    var entity = entityCollection.getEntity("Department");

    entity.findOne({
        _id: id
    }, null, function (err, record) {
        if (err) {
            var newErr = new Error();
            newErr.leval = 9;
            newErr.title = '查询部门信息错误';
            newErr.message = '请检查查询参数！';
            completeFun(newErr);
        } else {
            completeFun(null, record);
        }
    });
};

/**
 * 通过上级部门查询部门
 * @param parentDepartment
 * @param completeFun
 */
exports.getDepartmentByParentDepartment = function (parentDepartment, completeFun) {
    var entity = entityCollection.getEntity("Department");
    entity.find({ParentDepartment: {$in: parentDepartment}, IsEffective: true},
        null,
        [{
            path: 'ParentDepartment',
            select: 'Code Name'
        }], function (err, records) {
            if (err) {
                var newErr = new Error();
                newErr.leval = 9;
                newErr.title = '查询部门错误';
                newErr.message = '请检查查询参数！';
                completeFun(newErr);
            } else {
                completeFun(null, records);
            }
        });
};
